﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace Orange.Logistics.Infrastructure {
    public class OrangeView : IView {

        public void Render(ViewContext viewContext, System.IO.TextWriter writer) {
            Write(writer, "----------Routing Data----------");
            foreach (var key in viewContext.RouteData.Values.Keys) {
                Write(writer, "key: {0}, value: {1},",
                    key, viewContext.RouteData.Values[key]);
            }

            Write(writer, "----------View Data----------");
            foreach (var key in viewContext.ViewData.Keys) {
                Write(writer, "key: {0}, value: {1},",
                    key, viewContext.ViewData[key]);
            }
        }

        private void Write(TextWriter writer, string template, params object[] values) {
            writer.Write(string.Format(template, values) + "<p/>");
        }
    }

    public class OrangeViewEngine : IViewEngine {

        public ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) {
            return new ViewEngineResult(new string[] { "Debug data view engine." });            
        }

        public ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) {
            if (viewName=="OrangeView") {
                return new ViewEngineResult(new OrangeView(), this);
            } else {
                return new ViewEngineResult(new string[] {"Debug data view engine." });
            }
        }

        public void ReleaseView(ControllerContext controllerContext, IView view) {
            //throw new NotImplementedException();
        }
    }
}